﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;

namespace RedemptoristRetreatCenter
{
    /// <summary>
    /// AddStaff class allows administrative users to add(register) staff members
    /// </summary>
    public partial class AddStaff : System.Web.UI.Page
    {
        /// <summary>
        /// On page load, binds list of state abbreviations to drop down menu 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            StateAbbreviations states = new StateAbbreviations();
            staffState.DataSource = states.getListOfStates();
            staffState.DataBind();
        }

        /// <summary>
        /// Checks to see if selected username already exists in database
        /// </summary>
        /// <param name="username"></param>
        /// <returns>Boolean</returns>
        private Boolean doesUserExist(String username)
        {
            UserAccountUtility userAccountUtil = new RedemptoristRetreatCenter.DatabaseUtility.UserAccountUtility();

            return userAccountUtil.userExists(username);
        }

        /// <summary>
        /// Checks to see if the password and confirm password match
        /// </summary>
        /// <param name="password1"></param>
        /// <param name="password2"></param>
        /// <returns>Boolean</returns>
        private Boolean passwordsMatch(String password1, String password2)
        {
            return password1.Equals(password2);
        }

        /// <summary>
        /// On username change, checks to see if the username exists
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Username_Change(object sender, EventArgs e)
        {
            if (doesUserExist(staffUserName.Text))
                ErrorMessage.Text = "Username already exists. Please choose a new username";
        }

        /// <summary>
        /// On password change, checks to see if password and confirm password match
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Password_ValidateMatch(object sender, EventArgs e)
        {
            if (!(passwordsMatch(staffPassword.Text, staffConfirmPassword.Text)))
                ErrorMessage.Text = "These passwords do not match. Please re-enter your password";
        }

        /// <summary>
        /// Upon clicking the Staff Register button, verify that username/passwords are valid and proceeds with registration functionality accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void StaffRegister_Click(object sender, EventArgs e)
        {
            UserAccountUtility userAccountUtil = new RedemptoristRetreatCenter.DatabaseUtility.UserAccountUtility();

            if (doesUserExist(staffUserName.Text))
                ErrorMessage.Text = "Username already exists. Please choose a new username";
            else if (!(passwordsMatch(staffPassword.Text, staffConfirmPassword.Text)))
                ErrorMessage.Text = "These passwords do not match. Please re-enter your password";
            else
            {
                UserAccountInfo newAccount = new UserAccountInfo(staffUserName.Text, staffPassword.Text, IsAdmin.Checked, true, false,
                                                                    staffFirstName.Text, staffLastName.Text, staffAddressLineOne.Text, staffAddressLineTwo.Text, staffCity.Text, staffState.Text,
                                                                    staffZipCode.Text, staffHomePhone.Text, staffCellPhone.Text, staffBusinessPhone.Text, staffEmail.Text, null,
                                                                    staffUserName.Text, System.DateTime.Now);
                if (userAccountUtil.insertUserAccount(newAccount, staffUserName.Text))
                    ErrorMessage.Text = "Your account has been created!";
                else
                    ErrorMessage.Text = "Uh oh, there was a problem creating your account.";
            }
        }
    }
}